package com.blue.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.blue.common.constant.CommonConstant;
import com.blue.common.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.system.MessagePO;
import com.blue.dto.system.MessageDTO;
import com.blue.service.system.IMessageService;
import com.blue.common.entity.ResponseResult;

import java.util.List;
import java.util.Map;


/**
 * 系统消息表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/system/message")
@Tag(name = "系统消息表")
public class MessageController {
    @Autowired
    private IMessageService messageService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list(MessageDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        MessagePO messagePO = ConvertUtils.convert(MessagePO.class, requestDTO);
        MPJQueryWrapper<MessagePO> queryWrapper = QueryWrapperUtils.initQueryWrapper(messagePO);
        queryWrapper.eq("t.recipient",ThreadLocalUtils.getLoginUserInfo().getUserId());
        Page<MessagePO> page = new Page<>(pageNum, pageSize);
        Page<MessagePO> pageResult = messageService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody MessageDTO requestDTO) {
        MessagePO messagePO = ConvertUtils.convert(MessagePO.class, requestDTO);
        messageService.save(messagePO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody MessageDTO requestDTO) {
        MessagePO messagePO = ConvertUtils.convert(MessagePO.class, requestDTO);
        messageService.updateById(messagePO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        messageService.removeBatchByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody MessageDTO requestDTO) {
        MessagePO messagePO = messageService.getById(requestDTO.getId());
        return ResponseResult.okResult(messagePO);
    }

    @PostMapping("/unReadCount")
    @Operation(summary = "未读消息数量")
    public ResponseResult<?> unReadCount() {
        Map<String,Long> countMap =  messageService.unReadCount();
        return ResponseResult.okResult(countMap);
    }

    @PostMapping("/read")
    @Operation(summary = "已读")
    public ResponseResult<?> read(@RequestBody List<String> ids) {
        messageService.update(new LambdaUpdateWrapper<MessagePO>()
                .set(MessagePO::getMsgStatus, CommonConstant.READ)
                .in(MessagePO::getId, ids));
        return ResponseResult.okResult();
    }
}
